package ij.hkust.AOTFController;

import ij.IJ;
import ij.Prefs;

import java.io.IOException;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;

public class AOTFClient {
	
	MyAOTFCtrInteface aotf;
	public static AOTFClient instants = new AOTFClient();
	public static Process process;
	
	private AOTFClient(){
		try {
			Runtime rm =Runtime.getRuntime();
//			process  = rm.exec("E:\\ProgramSpace\\jre7\\bin\\java.exe -Djava.security.manager -Djava.security.policy=E:/ProgramSpace/my.policy -jar C:\\Users\\storm\\Desktop\\AOTFCtr.jar");
			process  = rm.exec("C:\\Users\\storm\\Desktop\\startAotf.bat");
//			if (System.getSecurityManager() == null) { 
//				System.setSecurityManager(new RMISecurityManager()); 
//			} 
			//Thread.currentThread().sleep(10000);
			
		} catch (Exception e) {
			IJ.log("shit...");
			e.printStackTrace();
		}

	}
	
	public static void main(String args[]){
		
		System.out.println(IJ.getDirectory("imageJ"));
		System.out.println(Prefs.getHomeDir());
		System.setSecurityManager(new RMISecurityManager()); 
		AOTFClient client = AOTFClient.instants;
		if(client.Open()){
			client.Write("dau dis\r");
			client.Write("dau en\r");
			client.Write("dau gain * 255\r");
//			client.Write("dds a 0 2000\r");
//			client.Write("dds a 1 2000\r");
//			client.Write("dds a 2 2000\r");
//			client.Write("dds a 3 2000\r");
//			client.Write("dds a 4 2000\r");
//			client.Write("dds a 5 2000\r");
			int count = 0;
			while(count < 500){
				client.Write("dds a 5 2000\r");
				client.Write("dds a 2 0\r");
				try {
					Thread.currentThread().sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				client.Write("dds a 2 2000\r");
				client.Write("dds a 5 0\r");
				try {
					Thread.currentThread().sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			client.Write("dds a 2 0\r");
			client.Write("dds a 5 0\r");
			client.Write("dau dis\r");
			client.Close();
		}
	}
	
	public boolean Open() {
		try {
			if(aotf == null){
				try {
					aotf = (MyAOTFCtrInteface) Naming.lookup("//127.0.0.1:8802/MYAOTFCTR");
				} catch (Exception e) {
					e.printStackTrace();
					IJ.log(e.toString());
					return false;
				}
			}
			return aotf.Open();
		} catch (RemoteException e) {
			e.printStackTrace();
			return false;
		}
	}

	public boolean Close() {
		try {
			if(aotf == null){
				return true;
			}
			return aotf.Close();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}

	public boolean Write(String command) {
		try {
			if(aotf == null){
				try {
					aotf = (MyAOTFCtrInteface) Naming.lookup("//127.0.0.1:8802/MYAOTFCTR");
					aotf.Open();
				} catch (Exception e) {
					e.printStackTrace();
					IJ.log(e.toString());
					return false;
				}
			}
			if(command == null){
				IJ.log("aotf write paramters is null!!");
			}
			return aotf.Write(command);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}

	public boolean IsOpen() {
		try {
			if(aotf == null){
				try {
					aotf = (MyAOTFCtrInteface) Naming.lookup("//127.0.0.1:8802/MYAOTFCTR");
					aotf.Open();
				} catch (Exception e) {
					e.printStackTrace();
					IJ.log(e.toString());
					return false;
				}
			}
			return aotf.IsOpen();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}

}
